<template>
  <view class="choose-community">
    <view class="current-location">
      <view class="title">
        当前地点
      </view>
      <view class="content">
        <text>{{currentLocation}}</text>
        <view @click="chooseLocation">
          <image src="../../static/icon/locate.png" />
          <text>重新定位</text>
        </view>
      </view>
      <view class="title">
        附近楼栋
      </view>
      <u-cell-group>
        <u-cell v-for='item in ComminityList' :key='item.id' :title="item.title" isLink="true" :name="item.title"
          url='/pkg-house/house-loudong/house-loudong' @click="a"></u-cell>
      </u-cell-group>
      <view class="title">
        个人设置
      </view>
    </view>
    <map :scale="16" @markertap="onMarker" :markers="markers" id="map" :longitude="location.longitude"
      :latitude="location.latitude"></map>
  </view>
</template>

<script>
  import qqmapwx from '@/utils/initQqMapWx.js'
  export default {
    data() {
      return {
        location: {
          longitude: 0,
          latitude: 0
        },
        markers: [{
          id: 1,
          longitude: 0,
          latitude: 0,
          width: 30,
          height: 40
        }],
        currentLocation: '定位中',
        ComminityList: []
      };
    },
    onLoad() {
      this.initLocation()
      this.searchComminity()
    },
    methods: {
      a(e) {
        this.$store.commit('user/setLoudong', e)
        uni.setStorageSync('loudong', e)
        console.log(e);
      },
      // uploadLocation 分装
      uploadLocation(res) {
        this.currentLocation = res.name
        this.location.longitude = res.longitude
        this.location.latitude = res.latitude
        this.markers[0].longitude = res.longitude
        this.markers[0].latitude = res.latitude
      },
      onMarker(e) {
        console.log(e)
        const house = this.markers.find((item) => {
          return item.id === e.markerId
        })
        console.log(house)
        // uni.navigateTo({
        // 	url: '/pkg-house/house-number/house-number'
        // })
      },
      // 重新选择定位
      chooseLocation() {
        uni.chooseLocation({
          ...this.location,
          success: (res) => {
            console.log(res)
            // 判断是否选择新的地址，若无则return掉
            if (!res.name) return
            this.currentLocation = res.name
            this.uploadLocation(res)
            // 搜索附近
            this.searchComminity()
          }
        })
      },
      // 初始化定位
      initLocation() {
        uni.getLocation({
          type: 'gcj02',
          isHighAccuracy: true,
          success: (res) => {
            // console.log(res)
            this.uploadLocation(res)
            // 将坐标解析文本
            this.initLocationText()
          },
          fail(err) {
            console.log(err)
          }
        })
      },
      // 将坐标解析文本
      initLocationText() {
        // console.log('坐标解析')
        // console.log(qqmapwx)
        qqmapwx.reverseGeocoder({
          location: this.location,
          success: (res, data) => {
            console.log(res.result)
            this.currentLocation = res.result.address
          }
        })
      },
      // 搜索附近
      searchComminity() {
        qqmapwx.search({
          keyword: "住宅",
          location: this.location,
          page_size: 20,
          success: (res) => {
            // console.log(res);
            this.ComminityList = res.data
            res.data.forEach(item => {
              this.markers.push({
                id: item.id - 0,
                longitude: item.location.lng,
                latitude: item.location.lat,
                width: 20,
                height: 20,
                iconPath: "/static/icon/house.png",
                title: item.title
              })
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  #map {
    width: 750rpx;
    height: 750rpx;
  }

  .choose-community {
    height: 100%;
    background-color: #FAFAFA;

    .current-location {
      .title {
        padding: 0 30rpx;
        line-height: 86rpx;
      }

      .content {
        width: 750rpx;
        height: 102rpx;
        background-color: #fff;
        box-sizing: border-box;
        padding: 0 30rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;

        text {
          font-size: 28rpx;
          color: #333;
        }

        view {
          display: flex;
          align-items: center;

          image {
            width: 30rpx;
            height: 30rpx;
            margin-right: 6rpx;
          }

          text {
            color: #5591AF;
          }
        }
      }
    }
  }
</style>
